home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / lanutsrc.zip / FASTNET.C < prev    next >
Text File  |  1992-07-16  |  9KB  |  374 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <malloc.h>
  6. #include <direct.h>
  7. #include <conio.h>
  8.  
  9. #include "lantasti.h"
  10. #include "lanctl.h"
  11.  
  12. #define LIST_SIZE 30
  13. #define NAME_SIZE 20
  14.            
  15. typedef struct FLAGS {
  16.         int suppress,
  17.             stop;
  18.          char fname[50];   
  19.          } FLAGS;
  20.  
  21. /* net commands supported by fastnet */
  22. #define NUM_NET_CMDS 5
  23. char *net_cmds[] = {
  24.     "USE",
  25.     "UNUSE",
  26.     "CLOCK",
  27.     "LPT",
  28.     "QUEUE",
  29.       };    
  30.       
  31. #define NUM_LPT_CMDS 4
  32. char *lpt_cmds[] = {
  33.      "TIMEOUT",
  34.      "COMBINE",
  35.      "FLUSH",
  36.      "SEPARATE"
  37.       };          
  38.       
  39. #define NUM_QUEUE_CMDS 6     
  40.   char *queue_cmds[] = {
  41.       "START",
  42.       "HALT",
  43.       "STOP",
  44.       "PAUSE",
  45.       "SINGLE",
  46.       "RESTART"
  47.       };
  48.  
  49.   FLAGS flags = {
  50.    FALSE,
  51.    FALSE,
  52.    ""
  53.  };
  54.  
  55. /* error ********************************************************************
  56.  
  57. *****************************************************************************/
  58. void error(code,message)
  59.   int code;
  60.   char *message;
  61. {
  62.   char *ptr;
  63.   
  64.   if (flags.suppress) return;
  65.   
  66.   if (code) {
  67.     ptr = get_error_text(code);
  68.     puts(ptr);
  69.   }
  70.   else puts(message);
  71. }
  72.  
  73. /* find_str ********************************************************************
  74. Returns -1 if target string not found
  75. *****************************************************************************/
  76. int find_str(string,array,n)
  77.   char *string,
  78.        *array[];
  79.    int n;
  80. {
  81.   int i,found;
  82.   
  83.   found = FALSE;
  84.   for (i = 0; i < n; i++ ) {
  85.     if (!strcmp(string,array[i])) {
  86.       found = TRUE;
  87.       break;
  88.     }
  89.   }
  90.   return((found) ? i : -1);
  91. }
  92.     
  93. /* net_use  ******************************************************************
  94.  
  95. *****************************************************************************/
  96. net_use(cmd)
  97.   char *cmd;
  98. {
  99.   char device[17],netpath[61],*ptr;
  100.   int type;
  101.   
  102.   type = 4;
  103.   
  104.   device[0] = netpath[0] = 0;
  105.   if (cmd != NULL) strcpy(device,cmd);
  106.  
  107.   if (strlen(device) > 2) {
  108.     if (!strncmp("LPT",device,3)) type = 3;
  109.     if (!strncmp("PRN",device,3)) type = 3;
  110.     if (!strncmp("COM",device,3)) type = 3;
  111.   }
  112.   
  113.   ptr = strtok(NULL," ");
  114.   if (ptr != NULL) strcpy(netpath,ptr);
  115.  
  116.   return(redirect_device(device,netpath,type));
  117. }
  118.  
  119. /* net_unuse  ****************************************************************
  120.  
  121. *****************************************************************************/
  122. net_unuse(cmd)
  123.   char *cmd;
  124. {
  125.   char device[17];
  126.  
  127.   device[0] = 0;
  128.   if (cmd != NULL) strcpy(device,cmd);
  129.  
  130.   return(cancel_redirection(device));
  131. }
  132.  
  133. /* net_clock ****************************************************************
  134.  
  135. *****************************************************************************/
  136. net_clock(cmd)
  137.   char *cmd;
  138. {
  139.  set_clock(cmd);
  140. }
  141.  
  142. /* net_lpt ******************************************************************
  143.  
  144. *****************************************************************************/
  145. net_lpt(cmd)
  146.   char *cmd;
  147. {
  148.   char *ptr;
  149.   int cmd_no,time,ret_code;
  150.   
  151.   ret_code = 0;
  152.   cmd_no = find_str(cmd,lpt_cmds,NUM_LPT_CMDS);
  153.   if (cmd_no >= 0) {
  154.     ptr = strtok(NULL," ");
  155.     switch (cmd_no) {
  156.       case 0:  /* timeout */
  157.         time = atoi(ptr);
  158.         ret_code = lpt_timeout(time);
  159.         break;
  160.       case 1:  /* combine */
  161.         set_lpt_mode(0);
  162.         break;
  163.       case 2:  /* flush */
  164.         flush_lpt();
  165.         break;
  166.       case 3:  /* separate */
  167.         set_lpt_mode(1);
  168.         break;
  169.     }      
  170.   }
  171.   else {
  172.     error(FALSE,"Invalid NET LPT command");
  173.     ret_code = -1;
  174.   }
  175.   return(ret_code);  
  176. }
  177.  
  178. /* net_queue ****************************************************************
  179.  
  180. *****************************************************************************/
  181. net_queue(cmd)
  182.   char *cmd;
  183. {
  184.   char *ptr;
  185.   int cmd_no,time,ret_code;
  186.   
  187.   ret_code = 0;
  188.   cmd_no = find_str(cmd,queue_cmds,NUM_QUEUE_CMDS);
  189.   if (cmd_no >= 0) {
  190.     ptr = strtok(NULL," ");
  191.     queue_ctl(cmd_no,ptr);
  192.   }
  193.   else {
  194.     error(FALSE,"Invalid NET QUEUE command");
  195.     ret_code = -1;
  196.   }
  197.   return(ret_code);  
  198. }
  199.  
  200. /* options */
  201. #define NUM_OPTIONS 3
  202. char *options[NUM_OPTIONS] = {
  203.     "STOP",
  204.     "SUPPRESS",
  205.     "HELP"
  206.   };
  207.  
  208. /* process_option ********************************************************************
  209.  
  210. *****************************************************************************/
  211. void process_option(string,flags)
  212.   char *string;
  213.   FLAGS *flags;
  214. {
  215.   char *ptr;
  216.   int i; 
  217.   
  218.   ptr = strtok(string,"=:");
  219.   
  220.   for (i = 0; i < NUM_OPTIONS; i++) {
  221.     if (!strcmpi(ptr,options[i])) break;
  222.   }
  223.   
  224.   if (i >= NUM_OPTIONS) {
  225.     printf("Whoops! %s isn't a valid command line option.\n",ptr);  
  226.     return;
  227.   }
  228.  
  229.   ptr = strtok(NULL," =");
  230.   
  231.   switch (i)  {
  232.     case 0:   /* stop on any error */
  233.       flags->stop = TRUE;
  234.       break;
  235.     case 1:   /* suppress error messages */
  236.       flags->suppress = TRUE;
  237.       break;
  238.     case 2:   /* help */
  239.       puts("FASTNET utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
  240.       puts("All rights reserved.  LANtastic is a trademark of Artisoft, Inc.\n");
  241.       puts("Usage: FASTNET <file name> [/OPTIONS]");
  242.       puts("FASTNET quickly processes entire files of the most commonly used");
  243.       puts("NET commands. Commands that FASTNET does not recognize will be");
  244.       puts("passed to your normal command interpreter.\n");
  245.       puts("Available options are:");
  246.       puts("/STOP         - stop on any error condition");
  247.       puts("/SUPPRESS     - suppress all error messages");
  248.       puts("/HELP         - display this documentation");
  249.       exit(0);
  250.       break;
  251.   }
  252. }
  253.  
  254. /* scan_command_line ********************************************************************
  255.  
  256. *****************************************************************************/
  257. scan_command_line(argc,argv,flags)
  258.   int argc;
  259.   char *argv[];
  260.   FLAGS *flags;
  261. {
  262.   int i,state;
  263.   state=0;
  264.   
  265.   for (i = 1;i < argc; i++) {
  266.     strupr(argv[i]);
  267.     if (argv[i][0] == '/') {
  268.       process_option(&argv[i][1],flags);
  269.       continue;
  270.     }
  271.     switch (state++) {
  272.       case 0:    /* first valid arg */
  273.         strcpy(flags->fname,argv[i]);
  274.  
  275. /* expand fname to ".BAT" if no extension is given */
  276.         if (strchr(flags->fname,'.') == NULL) strcat(flags->fname,".BAT");
  277.         break;
  278.       default:
  279.         error(FALSE,"Extra command line argument ignored.");
  280.         break;  
  281.     }
  282.   }
  283. }
  284.  
  285. /* do_net_cmd ********************************************************************
  286.  
  287. *****************************************************************************/
  288. do_net_cmd(cmd)
  289.   char *cmd;
  290. {
  291.   int lastchar,cmd_no,ret_code;
  292.   char *ptr,cmd_cpy[81];
  293.   
  294.   ret_code = 0;
  295.   lastchar = strlen(cmd) - 1;
  296.   if (cmd[lastchar] == '\n') cmd[lastchar] = 0;
  297.   strupr(cmd);
  298.   strcpy(cmd_cpy,cmd);
  299.  
  300. /* see if we've got a net command or some other batch command */  
  301.   ptr = strtok(cmd," ");
  302.   if (!strcmp(ptr,"NET")) {
  303.     ptr = strtok(NULL," ");
  304.     cmd_no = find_str(ptr,net_cmds,NUM_NET_CMDS);
  305.     if (cmd_no >= 0) {
  306.       ptr = strtok(NULL," ");
  307.       switch (cmd_no) {
  308.         case 0:       /* use */
  309.           ret_code = net_use(ptr);
  310.           break;
  311.         case 1:       /* unuse */
  312.           ret_code = net_unuse(ptr);
  313.           break;
  314.         case 2:       /* clock */
  315.           net_clock(ptr);
  316.           break;
  317.         case 3:       /* lpt */
  318.           net_lpt(ptr);
  319.           break;
  320.         case 4:       /* queue */
  321.           net_queue(ptr);
  322.           break;
  323.       }
  324.     } else system(cmd_cpy);
  325.   } else system(cmd_cpy);
  326.   return(ret_code);
  327. }
  328.  
  329. /* main ********************************************************************
  330.  
  331. *****************************************************************************/
  332. int main(argc,argv)
  333.   int argc;
  334.   char *argv[];
  335. {
  336.   char buffer[1024];
  337.   FILE *cmdfile;
  338.   char cmd[81];
  339.   int ret_code;
  340.  
  341. #ifdef SHAREWARE
  342.   puts("FASTNET utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
  343.   puts("All rights reserved.  Thanks for trying this unregistered Shareware edition!\n");
  344. #endif  
  345.  
  346.   scan_command_line(argc,argv,&flags);  
  347.   
  348.   if (!nos_present()) {
  349.     error(FALSE,"LANtastic is not currently running.");
  350.     return(-1);
  351.   }
  352.  
  353.   cmdfile = fopen(flags.fname,"rt");
  354.   if (cmdfile != NULL) {
  355.     setvbuf(cmdfile,buffer,_IOFBF,sizeof(buffer));
  356.     fgets(cmd,80,cmdfile);
  357.     while (!feof(cmdfile)) {
  358.       ret_code = do_net_cmd(cmd);
  359.       if (ret_code) {
  360.         error(ret_code,NULL);
  361.         if (flags.stop) break;
  362.       }
  363.       fgets(cmd,80,cmdfile);
  364.     }
  365.     fclose(cmdfile);
  366.   }
  367.   else {
  368.     sprintf(buffer,
  369.     "Sorry, I can't open the NET command file. (%s)\n",flags.fname);
  370.     error(FALSE,buffer);
  371.   }
  372.   return(ret_code);
  373. }
  374.